package com.fastloan.mgt.customer.app;

import com.fastloan.mgt.customer.model.ZyCustomer;
import com.fastloan.mgt.customer.service.ZyCustomerService;
import com.fastloan.mgt.product.basics.model.vo.ProductHotExt;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.sys.model.CmuSysFile;
import com.fastloan.safe.mgt.sys.model.vo.AddressFiled;
import com.fastloan.safe.mgt.sys.service.CmChinaCityService;
import com.fastloan.safe.mgt.sys.service.CmSysFileService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.github.pagehelper.PageInfo;
import com.kawins.base.baseabs.BaseController;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by gelon on 2017/11/9.
 */
@RestController
@RequestMapping("/api/wxapp/customer")
public class AppCustomerController extends BaseController<ZyCustomer> {


    @Autowired
    ZyCustomerService customerService;

    @Autowired
    ProductService productService;

    @Autowired
    CmChinaCityService chinaCityService;

    @Autowired
    CmSysFileService fileService;

    @Autowired
    CmuUserService userService;

    /**
     * 获取客户信息
     */
    @GetMapping
    public ResultTo customerInfo() {
        return new ResultTo()
                .setData(customerService.selectByPrimaryKey(ManagerKit.getCustomerId()));
    }

    /**
     * 该地区所有产品
     *
     * @param addressFiled 地理信息
     * @param maxAmount    额度区间
     * @param minAmount    额度区间
     * @param monthlyOrder 排序
     */
    @GetMapping("/sellProduct")
    public ResultTo sellProduct(AddressFiled addressFiled, BigDecimal maxAmount,
                                BigDecimal minAmount, Integer monthlyOrder) {
        addressFiled = chinaCityService.selectAllIdByAdd(addressFiled);
        List<Integer> areaIds = getAreaIdS(addressFiled);
        if (areaIds.isEmpty()) {
            return new ResultTo(ResultEnum.OPERATION_FAILED, "未获取到定位信息");
        }
        if (maxAmount == null) {
            maxAmount = new BigDecimal(99999999);
        }
        if (minAmount == null) {
            minAmount = BigDecimal.ZERO;
        }
        if (monthlyOrder == null) {
            monthlyOrder = 0;
        }
        String order = monthlyOrder == 0 ? "ASC" : "DESC";
        Map<String, Object> map = new HashMap<>(5);
        map.put("areaIds", areaIds);
        map.put("maxAmount", maxAmount);
        map.put("minAmount", minAmount);
        map.put("order", order);
        map.put("date", new Date());
        ResultTo resultTo = dataPage(productService, ProductHotExt.class, "queryAddressProduce",
                map);
        PageInfo<ProductHotExt> pageInfo = (PageInfo<ProductHotExt>) resultTo.get("data");
        pageInfo.getList().forEach(this::putUrl);
        return resultTo;
    }

    private void putUrl(ProductHotExt productHotExt) {
        if (productHotExt.getImgId() != null) {
            CmuSysFile sysFile = fileService.selectByPrimaryKey(productHotExt.getImgId());
            if (sysFile != null) {
                productHotExt.setImgUrl(
                        sysFile.getFileServer() + sysFile.getGroupName() + "/" + sysFile
                                .getRemoteFileName());
            }
        }
        if (productHotExt.getLogoId() != null) {
            CmuSysFile sfile = fileService.selectByPrimaryKey(productHotExt.getLogoId());
            if (sfile != null) {
                productHotExt.setLogoUrl(
                        sfile.getFileServer() + sfile.getGroupName() + "/" + sfile.getRemoteFileName());
            }
        }
    }


    /**
     * 首页滚动图
     *
     * @param addressFiled 地理信息
     */
    @GetMapping("/rollingGraph")
    public ResultTo rollingGraph(AddressFiled addressFiled) {
        addressFiled = chinaCityService.selectAllIdByAdd(addressFiled);
        List<Integer> areaIds = getAreaIdS(addressFiled);
        Map<String, Object> map = new HashMap<>();
        map.put("areaIds", areaIds);
        map.put("date", new Date());
        List<ProductHotExt> productHotExts = productService.selectRollingGraph(map);
        productHotExts.forEach(this::putUrl);
        return new ResultTo().setData(productHotExts);
    }

    /**
     * 首页产品表
     */
    @GetMapping("/hotSell")
    public ResultTo hotSell(AddressFiled addressFiled) {
        addressFiled = chinaCityService.selectAllIdByAdd(addressFiled);
        List<Integer> areaIds = getAreaIdS(addressFiled);
        Map<String, Object> map = new HashMap<>();
        map.put("areaIds", areaIds);
        map.put("date", new Date());
        List<ProductHotExt> productHotExts = productService.queryHotSell(map);
        productHotExts.forEach(this::putUrl);
        return new ResultTo().setData(productHotExts);
    }


    /**
     * 根据客户经理号码给客户绑定客户经理
     */
    @PostMapping("/bindManager")
    public ResultTo bindManager(String managerCode) {
        CmuUser user = customerService.bindManager(managerCode);
        if (user != null) {
            return new ResultTo().setData(user);
        }
        return new ResultTo(ResultEnum.OPERATION_FAILED, "绑定不是客户经理");
    }


    private List<Integer> getAreaIdS(AddressFiled addressFiled) {
        List<Integer> areaIds = new ArrayList<>();
        if (addressFiled.getProvinceId() != null) {
            areaIds.add(addressFiled.getProvinceId());
        }
        if (addressFiled.getCityId() != null) {
            areaIds.add(addressFiled.getCityId());
        }
        if (addressFiled.getAreaId() != null) {
            areaIds.add(addressFiled.getAreaId());
        }
        return areaIds;
    }

    /**
     * 获得客户的客户经理
     */
    @GetMapping("/mineManager")
    public ResultTo mineManager() {
        ZyCustomer customer = customerService.selectByPrimaryKey(ManagerKit.getCustomerId());
        return new ResultTo()
                .setData(userService.selectByPrimaryKey(customer.getCustomerManagerId()));
    }


}
